$(function () {
    //变量抽取
    var clientRepaymentInfo_dataGrid = $("#clientRepaymentInfo_dataGrid");
    var setOpinion_dialog = $("#setOpinion_dialog");
    var setOpinion_form = $("#setOpinion_form");

    clientRepaymentInfo_dataGrid.datagrid({
        url: "/clientRepaymentInfo/query.do",
        fitColumns: true,//列自适应
        fit: true,
        columns: [[
            {
                field: 'status', title: '通知单单据状态', width: 100, formatter: function (value, row, index) {
                    if (row.status == 0) {
                        return "未通知客户";
                    } else if (row.status == 1) {
                        return "已通知客户";
                    }
                }
            },
            {field: 'repaymentInfoNumber', title: '通知单单号', width: 100},
            {field: 'caseNumber', title: '报案单据号', width: 100},
            {field: 'insuranceBillNumber', title: '保险单号', width: 100},
            {
                field: 'handler', title: '通知单负责人', width: 100, formatter: function (value, row, index) {
                    return row.handler.realname;
                }
            },
            {field: 'companyName', title: '保险公司', width: 100},
            {field: 'opinion', title: '公司意见', width: 100},
            {
                field: 'client', title: '客户姓名', width: 100, formatter: function (value, row, index) {
                    return row.client.clientName;
                }
            },
        ]],
        toolbar: "#tb",//工具
        pagination: true,//分页
        rownumbers: true,//行号
        singleSelect: true,//只允许选择一行
    })


    setOpinion_dialog.dialog({
        width: 300,
        height: 200,
        draggable: true,
        resizable: true,
        buttons: "#btns",
        closed: true,
        onClose: function () {
            setOpinion_form.form("clear");
        }
    })


    //方法抽成对象统一管理
    var methodObj = {
        //新增公司意见
        setOption: function () {
            var row = clientRepaymentInfo_dataGrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('提示', '请选择客户理赔通知单', 'info');
            } else if (row.status != 0) {
                $.messager.alert('提示', '当前理赔通知单已通知客户,不可以再添加意见', 'info');
            }
            else {
                setOpinion_form.form('load', '/clientRepaymentInfo/queryByInfoSn.do?infoSn=' + row.repaymentInfoNumber);
                setOpinion_dialog.dialog("open");
            }
        },

        //通知客户按钮
        inform: function () {
            var row = clientRepaymentInfo_dataGrid.datagrid("getSelected");

            if (!row) {
                $.messager.alert('提示', '请选择客户理赔通知单', 'info');
            } else if (row.status != 0) {
                $.messager.alert('提示', '当前理赔通知单已通知', 'info');
            } else {
                $.get('/clientRepaymentInfo/informClient.do', {repaymentInfoNumber: row.repaymentInfoNumber}, function (data) {
                    if (data.success) {
                        $.messager.alert('提示', '通知客户成功', 'info')
                        clientRepaymentInfo_dataGrid.datagrid("reload");
                    } else {
                        $.messager.alert("提示", data.msg, 'error');
                    }
                });
            }
        },

        reload: function () {
            clientRepaymentInfo_dataGrid.datagrid("load");
        },


        save: function () {
            var row = clientRepaymentInfo_dataGrid.datagrid("getSelected");
            setOpinion_form.form("submit", {
                url: "/clientRepaymentInfo/setOpinion.do?infoSn=" + row.repaymentInfoNumber,
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('提示', '保存成功', 'info', function () {
                            setOpinion_dialog.dialog("close");
                            setOpinion_form.datagrid("reload");
                        });
                    } else {
                        $.messager.alert('提示', '保存失败', 'warining');
                    }
                    clientRepaymentInfo_dataGrid.datagrid("reload");
                }
            })
        },


        //取消按钮
        cancel: function () {
            setOpinion_dialog.dialog("close")
        },

    }

    //按钮统一绑定事件
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd]();
    });
});

//搜索按钮
function mySearch() {
    //获取到查询条件的值
    var status = $("#status").combobox('getValue');
    $("#clientRepaymentInfo_dataGrid").datagrid('load', {status: status});
}